//
//  HWAddressSelectionModel.m
//  OCDome
//
//  Created by 李含文 on 2020/8/31.
//  Copyright © 2020 李含文. All rights reserved.
//

#import "HWAddressSelectionModel.h"



@implementation HWAddressCityModel

- (instancetype)initWithDictionary:(NSDictionary*)otherDictionary{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:otherDictionary];
    }
    return self;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    NSLog(@"undefine %@",key);
}

@end

@implementation HWAddressProvinceModel

- (instancetype)initWithDictionary:(NSDictionary*)otherDictionary{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:otherDictionary];
    }
    return self;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    NSLog(@"undefine %@",key);
}

- (void)setValue:(id)value forKey:(NSString *)key{
    [super setValue:value forKey:key];
    if ([key isEqualToString:@"cityList"]) {
        NSMutableArray<HWAddressCityModel*> * childrenArray = [[NSMutableArray alloc] init];
        for (NSDictionary *dic in value) {
            HWAddressCityModel * children = [[HWAddressCityModel alloc] initWithDictionary:dic];
            children.title = children.name;
            [childrenArray addObject:children];
        }
        HWAddressCityModel *first = childrenArray.firstObject;
        if (first) {
            if ([first.name isEqualToString:self.name]) {
                [childrenArray removeObject:first];
            }
        }
        HWAddressCityModel *model = [[HWAddressCityModel alloc] init];
        model.name = @"不限";
        model.code = self.code;
        model.title = self.name;
        [childrenArray insertObject:model atIndex:0];
        self.cityList = childrenArray;
    }
}
@end

@implementation NSArray (JSON)
+ (NSArray *)loadJsonOfBundlePathForResource:(NSString*)name{
    NSString *provincesPath = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
    NSData *data = [[NSData alloc] initWithContentsOfFile:provincesPath];
    NSArray *temArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    return temArray;
}
@end

@implementation HWAddressSelectionModel
- (instancetype)init{
    self = [super init];
    if (self) {
        NSArray * temArray = [NSArray loadJsonOfBundlePathForResource:@"city"];
        NSMutableArray<HWAddressProvinceModel*> * provinceArray = [[NSMutableArray alloc] init];
        HWAddressProvinceModel * provinces = [[HWAddressProvinceModel alloc] init];
        provinces.name = @"全国";
        provinces.code = @"";
        HWAddressCityModel *model = [[HWAddressCityModel alloc] init];
        model.name = @"不限";
        model.title = @"全国";
        model.code = @"";
        provinces.cityList = @[model];
        [provinceArray addObject:provinces];
        for (NSDictionary *dic in temArray) {
            HWAddressProvinceModel * provinces = [[HWAddressProvinceModel alloc] initWithDictionary:dic];
            [provinceArray addObject:provinces];
        }
        self.provinces = provinceArray;
    }
    return self;
}

@end


